package main

import (
	"fmt"
	"reflect"
	"time"
)

type MethInfo struct {
	ind  int
	name interface{}
	args []interface{}
	sign int //0:未运行；1：正在运行；2：运行完成
}
type MessageQueue struct {
	methodQueue []*MethInfo
	count       int
	sum         int
	sign        bool
}

func (this MessageQueue) init(count int, sum int) (err error) {
	this.count = count
	this.sum = sum
	this.sign = true
	this.methodQueue = make([]*MethInfo, 0, sum)
	return
}

//一个存储，一个运行
func (this MessageQueue) inQueue(method interface{}, args []interface{}) {
	var methInfo = &MethInfo{0, method, args, 0}
	this.methodQueue = append(this.methodQueue, methInfo)
	this.count++
}

//运行
func (this MessageQueue) run() {
	go func() {
		for this.sign {
			methInfo := this.methodQueue[0]
			if methInfo.sign != 0 {
				this.methodQueue = this.methodQueue[1:]
				continue
			}
			methInfo.sign = 1
			this.methodRun(methInfo)
			this.methodQueue = this.methodQueue[1:]
			methInfo.sign = 2
		}
	}()
}

//函数运行
func (this MessageQueue) methodRun(meth *MethInfo) {
	fmt.Println("运行方法", meth.ind)
	meth.sign = 1
	fun := reflect.ValueOf(meth.name)
	var listVal []reflect.Value
	for _, v := range meth.args {
		listVal = append(listVal, reflect.ValueOf(v))
	}
	fun.Call(listVal)
	fmt.Println("运行方法结束", meth.ind)
	meth.sign = 2
}

//关闭
func (this MessageQueue) Close() {
	for len(this.methodQueue) != 0 {
		time.Sleep(10)
	}
	this.sign = false
}
func main() {

}
